<?php

class Group extends AppModel {
	var $name = "Group";
	
	//<!--- this part comes from kitchen-1.0.1.zip from aranworld.com
	var $actsAs = array('Acl'=>'requester');

    /**
     * Allows the AclBehavior to determine parental ownership of
     * currently active record.
     *
     * @access public
     * @returns array data array to be used by AclBehavior for node lookup
     */
    function parentNode(){
		if (!$this->id) {
		  return null;
		}

		$data = $this->read();

		if (!$data['Group']['parent_id']){
		  return null;
		} else {
		  return array('model' => 'Group', 'foreign_key' => $data['Group']['parent_id']);
		}
    }

    /**
     * processes completed after saving record
     *
     */
	function afterSave($created = null){
	    if( $created ){
		    $this->id = $this->getLastInsertId();
		    // first create alias for the newly created Aro
			$aroId = $this->Aro->getLastInsertId();
			$this->Aro->create();
			$this->Aro->id = $aroId;
			if( $this->Aro->saveField('alias', $this->data['Group']['name'] ) ){
				return TRUE;
			} else {
				return FALSE;
			}
		} else {
		    if( $this->data['Group']['parent_id'] !== $this->data['Group']['old_parent_id'] ){  // <!--- this requires that old_parent_id must be in form to update user group id.
				// what is the id of the aro row that has $this->data['Group']['parent_id'] as it's foreign_key? 
				$parentInfo = $this->Aro->find(array('foreign_key'=>$this->data['Group']['parent_id'], 'model'=>'Group') );
				$groupAro = $this->Aro->find(array('foreign_key'=>$this->data['Group']['id'], 'model'=>'Group') );
				$updatedAro = array(
					'Aro' => array(
						'id' => $groupAro['Aro']['id'],
						'parent_id' => $parentInfo['Aro']['id']
						)
					);
				$this->Aro->save( $updatedAro );
			}
		}
	//<!--- up to this part
	
	}
	
}	

?>